package DAO;


import java.sql.Date;
import java.util.List;
import java.util.Vector;



import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import hibernate.AssetInfo;
import hibernate.DictInfo;
import hibernateUtil.HibernateUtil;

public class DictInfoDAO {
	public int count = 0;
	@SuppressWarnings("unchecked")
	public List<String> getAllDepartment()
	{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			Transaction trans = session.beginTransaction();
			List l = session.createQuery("from DictInfo where type='部门'").list();
			trans.commit();
			return l;
	}
	@SuppressWarnings("unchecked")
	/*
	 * 参数 -- 资产类型
	 * 通过参数得到所有的资产类型
	 */
	public List getAllAssetType()
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		List list = session.createQuery("select distinct deviceType from AssetInfo").list();
		trans.commit();
		return list;
	}
	public List getName()
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		List list = session.createQuery("select name from DictInfo").list();
		trans.commit();
		
		return list;
	}	
    
	public List getAllDict(int start,int limit){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
				
		Query query = session.createQuery("from DictInfo");
		count = query.list().size();
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
		trans.commit();		
		return l;		
	};
    @SuppressWarnings("unchecked")

    public List getDictByQuery(int start,int limit,String name,String type)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from DictInfo where ";		
		if(!"".equals(name))
		{
			str = str + "name=:name ";
		}
		if(!"".equals(type))
		{
			if(!"".equals(type))
			{
				str = str + "or type=:type ";
			}
			else
			{
				str = str + "type=:type ";
			}
		}		
	
		Query query = session.createQuery(str);  
		
		if(!"".equals(name))
		{
			query.setString("name", name);
		}
		if(!"".equals(type))
		{
			query.setString("type", type);
		}		
		count = query.list().size();
		query.setFirstResult(start);  
		query.setMaxResults(limit); 	

		List l = query.list();
		trans.commit();
		
		return l;
    }   

    public void addDictInfo(String name, String type){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		DictInfo dictInfo = new DictInfo();
		dictInfo.setName(name);
		dictInfo.setType(type);
		session.save(dictInfo);
		trans.commit();
    }
    
    public void updateDictInfo(int dict_id,String name, String type){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		Query q = session.createQuery("update DictInfo set name=:name,type=:type where dict_id=:dict_id");
		q.setString("name",name);
		q.setString("type",type);
		q.setInteger("dict_id",dict_id);
		q.executeUpdate();
		trans.commit();    	
    }
    public void deleteDictInfo(Vector dict_id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		for(int i=0; i<dict_id.size(); i++ )
		{
			DictInfo assetInfo = (DictInfo)session.get(DictInfo.class,Integer.parseInt((String)dict_id.get(i)));
			session.delete(assetInfo);
		}
		
		trans.commit();    	
    }
}
